<template>
  <div
    v-if="isVisiabled"
    class="overDiv"
  >
    <div
      v-if="isVisiabled"
      class="modelBox"
    >
      <div class="header">
        <div class="titles">
          新建地址
        </div>
        <div
          class="close"
          @click="hideModal"
        >
          <img
            src="../../assets/img/close.png"
            alt=""
          >
        </div>
      </div>
      <el-form
        :inline="true"
        :label-position="labelPosition"
        label-width="80px"
        class="demo-form-inline"
      >
        <div class="body">
          <div>
            <el-form-item label="所在地区:">
              <el-select
                v-model="province"
                placeholder="请选择"
                @change="changeProvince"
              >
                <el-option
                  v-for="item in provinceList"
                  :key="item.regionCode"
                  :label="item.name"
                  :value="item.regionCode"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="">
              <el-select
                v-model="city"
                placeholder="请选择"
                @change="changeCity"
              >
                <el-option
                  v-for="item in cityList"
                  :key="item.regionCode"
                  :label="item.name"
                  :value="item.regionCode"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="">
              <el-select
                v-model="area"
                placeholder="请选择"
                @change="changeArea"
              >
                <el-option
                  v-for="item in areaList"
                  :key="item.regionCode"
                  :label="item.name"
                  :value="item.regionCode"
                />
              </el-select>
            </el-form-item>
          </div>
          <div>
            <el-form-item label="详细地址:">
              <el-input
                v-model="address"
                type="textarea"
                class="textarea"
                :rows="6"
              />
            </el-form-item>
          </div>
          <div>
            <el-form-item label="收货人:">
              <el-input
                v-model="rename"
                placeholder=""
              />
            </el-form-item>
            <el-form-item label="手机号码:">
              <el-input
                v-model="phone"
                placeholder=""
                type="phone"
                :maxlength="11"
              />
            </el-form-item>
          </div>
          <el-form-item label=" ">
            <img
              :src="delivery ? on : off"
              style="vertical-align: middle"
              @click="setInit"
            >

            <span style="padding-left: 0.5rem">设为默认</span>
          </el-form-item>
        </div>

        <div class="footer">
          <div
            class="btn correct"
            @click="onSubmit"
          >
            确认
          </div>
          <div
            class="btn"
            @click="hideModal"
          >
            取消
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import on from "../../assets/img/radio_on.png";
import off from "../../assets/img/radio.png";
export default {
  // props: ["edititem"],
  props: {
    edititem:{
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      labelPosition: "right",
      isVisiabled: false,
      on: on,
      off: off,
      provinceList: [],
      province: "",
      cProvince: "",
      cityList: [],
      city: "",
      cCity: "",
      areaList: [],
      area: "",
      cArea: "",
      address: "",
      delivery: true,
      rename: "",
      phone: "",
      currentItems: {},
      addressId: "",
      status: "",
    };
  },
  watch: {
    edititem: function (val) {
      this.currentItems = val;
    },
  },
  mounted() {
    this.getProvinces();
  },
  methods: {
    //提交
    onSubmit() {
      if (this.province == "") {
        this.$message.error("请选择省");
        return false;
      } else {
        this.provinceList.forEach((item) => {
          if (item.regionCode == this.province) {
            this.cProvince = item.name;
          }
        });
      }
      if (this.city == "") {
        this.$message.error("请选择市");
        return false;
      } else {
        this.cityList.forEach((item) => {
          if (item.regionCode == this.city) {
            this.cCity = item.name;
          }
        });
      }
      if (this.area == "") {
        this.$message.error("请选择区");
        return false;
      } else {
        this.areaList.forEach((item) => {
          if (item.regionCode == this.area) {
            this.cArea = item.name;
          }
        });
      }
      let reg = /^1[3456789]\d{9}$/;
      if (this.rename == "") {
        this.$message.error("收货人必填");
        return false;
      } else if (this.phone == "") {
        this.$message.error("手机号码必填");
        return false;
      } else {
        if (!reg.test(this.phone)) {
          this.$message.error("请填写正确的手机号码");
          return false;
        } else {
          let params;

          if (this.status == "add") {
            params = {
              cProvince: this.cProvince,
              cCity: this.cCity,
              cArea: this.cArea,
              cDistrct: this.cProvince + this.cCity + this.cArea,
              cAddress: this.address,
              cReceiver: this.rename,
              cMobile: this.phone,
              bDefault: this.delivery,
              cRegion: this.area,
            };
          } else if (this.status == "edit") {
            let item = this.currentItems;
            console.log(item);

            //依次获取省市区列表
            // cProvince
            //根据区的id获取父级
            // this.areaList.forEach(items=>{
            // console.log(items.regionCode)
            // if(area.regionCode ==  item.cRegion){
            // 	this.pcity = area.parent
            // 	//根据市的id获取父级
            // 	this.cityList.forEach(city=>{
            // 		if(city.regionCode ==  area.parent){
            // 			this.pprovince = city.parent
            // 			this.provinceList.forEach(items=>{
            // 				if(items.regionCode ==  city.parent){
            // 					console.log(items.name,city.name,area.name)
            // 				}

            // 			})
            // 		}
            // 	})
            // }
            // })

            params = {
              cProvince: this.cProvince,
              cCity: this.cCity,
              cArea: this.cArea,
              cDistrct: this.cProvince + this.cCity + this.cArea,
              cAddress: this.address,
              cReceiver: this.rename,
              cMobile: this.phone,
              bDefault: this.delivery,
              id: this.addressId,
              ts: item.ts,
              iMemberId: item.iMemberId,
              isCashOnDelivery: item.isCashOnDelivery,
              isForbidden: item.isForbidden,
              elValuesNotExists: item.elValuesNotExists,
              cRegion: item.cRegion,
              cCountry: item.cCountry,
            };
          }
          console.log(params);
          this.isVisiabled = false;
          this.$emit("saveAddress", params);
        }
      }
    },
    //获取省
    getProvinces() {
      this.$api.person.getProvinces({}).then((res) => {
        // 执行某些操作
        if (res.code == 200) {
          // console.log(res.data);
          this.provinceList = res.data;
        }
      });
    },
    //获取 市
    getCitysFromProvince(parentId) {
      this.$api.person
        .getCitysFromProvince({
          parentId: parentId,
        })
        .then((res) => {
          // 执行某些操作
          if (res.code == 200) {
            // console.log(res.data);
            this.cityList = res.data;
          }
        });
    },
    //获取 区
    getDistrictFromCity(parentId) {
      this.$api.person
        .getDistrictFromCity({
          parentId: parentId,
        })
        .then((res) => {
          // 执行某些操作
          if (res.code == 200) {
            // console.log(res.data);
            this.areaList = res.data;
          }
        });
    },
    //选择省
    changeProvince() {
      console.log(this.province);
      this.getCitysFromProvince(this.province);
      this.city = "";
      this.area = "";
    },
    //选择市
    changeCity() {
      this.getDistrictFromCity(this.city);
      this.area = "";
    },
    //选择区
    changeArea(e) {
      console.log(e);
      this.area = e;
    },
    showM(value, status) {
      console.log(value, status);
      this.status = status;
      if (status == "add") {
        this.province = "";
        this.city = "";
        this.area = "";
        this.address = "";
        this.rename = "";
        this.phone = "";
        this.delivery = true;
      } else if (status == "edit") {
        this.$nextTick(function () {
          console.log(this.currentItems);
          let currentItems = this.currentItems;
          this.province = currentItems.cProvince;
          this.city = currentItems.cCity;
          this.area = currentItems.cArea;
          this.address = currentItems.cAddress;
          this.rename = currentItems.cReceiver;
          this.phone = currentItems.cMobile;
          this.delivery = currentItems.bDefault;
          this.addressId = currentItems.id;
        });
      }
      this.isVisiabled = value;
    },
    //隐藏
    hideModal() {
      this.isVisiabled = false;
    },
    //设置默认地址
    setInit() {
      this.delivery = !this.delivery;
    },
  },
};
</script>

<style scoped lang="scss">
.el-switch.is-checked .el-switch__core {
  background: #f0406f !important;
  border-color: #f0406f !important;
}

.el-form-item {
  margin-bottom: 0;
}

.form {
  padding: 6.25rem 0 4.13rem;
  text-align: center;
}

.overDiv {
  position: fixed;
  height: 100%;
  width: 100%;
  left: 0;
  top: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 1000;
}

.modelBox {
  position: absolute;
  // height: 35.13rem;
  width: 58rem;
  padding: 1.25rem;
  left: 50%;
  margin-left: -29rem;
  top: 50%;
  margin-top: -17.06rem;
  background: rgba(255, 255, 255, 1);
  z-index: 1001;

  .header {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;

    .titles {
      font-size: 1.13rem;
      color: #666666;
      padding: 0.4rem 0 0 0.5rem;
    }

    .close {
      width: 1.88rem;
      height: 1.88rem;

      > img {
        width: 100%;
        height: 100%;
      }
    }
  }

  .body {
    padding: 3.56rem 2.5rem 1.94rem;

    > div {
      padding-bottom: 1.25rem;
    }

    .selectAddress {
      > input {
        width: 13.44rem;
        height: 2.44rem;
        background: rgba(255, 255, 255, 1);
        border: 0.06px solid rgba(210, 210, 210, 1);
      }
    }

    .textarea {
      overflow: hidden;
      width: 42.94rem;

      .el-textarea__inner {
        width: 100%;
        background: rgba(255, 255, 255, 1);
        border: 0.06px solid rgba(210, 210, 210, 1);
      }
    }
  }

  .footer {
    display: flex;
    padding-left: 7.5rem;
    flex-direction: row;

    .btn {
      width: 8.25rem;
      height: 2.56rem;
      background: rgba(240, 64, 111, 1);
      color: #fff;
      text-align: center;
      line-height: 2.56rem;
      font-size: 1rem;
    }

    .correct {
      background: rgba(0, 0, 0, 1);
      color: #fff;
      margin-right: 0.94rem;
    }
  }
}
</style>
